package com.casic.ops.controller;

import com.alibaba.fastjson.JSON;
import com.casic.ops.OpsApiApplication;
import com.casic.ops.annotation.RestWrapper;
import com.casic.ops.consts.user.RoleType;
import com.casic.ops.entity.dto.UserDTO;
import com.casic.ops.service.api.CommonService;
import com.casic.ops.utils.Currents;
import com.orion.utils.Exceptions;
import com.orion.utils.io.StreamReaders;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @ClassName ops-platform
 * @Description 公共
 * @Author mianbin
 * @Date 2022/5/31 10:58
 * @Version 1.0
 **/
@RestController
@RestWrapper
@RequestMapping("/ops/api/common")
public class CommonController {

    @Resource
    private CommonService commonService;

    /**
     * 菜单
     */
    @RequestMapping("/menu")
    public List<?> getMenu() throws IOException {
        UserDTO user = Currents.getUser();
        String menuFile;
        if (RoleType.ADMINISTRATOR.getType().equals(user.getRoleType())) {
            menuFile = "menu-admin.json";
        } else if (RoleType.DEVELOPER.getType().equals(user.getRoleType())) {
            menuFile = "menu-dev.json";
        } else if (RoleType.OPERATION.getType().equals(user.getRoleType())) {
            menuFile = "menu-opt.json";
        } else {
            throw Exceptions.app();
        }
        InputStream menu = OpsApiApplication.class.getResourceAsStream("/menu/" + menuFile);
        return JSON.parseArray(new String(StreamReaders.readAllBytes(menu)));
    }

}
